<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/06/10
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Db;
use think\Model;

/**
 * 调拨管理
 * @package app\erp\model
 */
class ErpGoodsWarehouseStockNum extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_goods_warehouse_stock_num';

    //查询库存价格
    public function get_goods_stock_price($store_id, $sku_id, $goods_id = 0){
        $sku_price = 0;

        $warehouse_list = model('ErpStoreWarehouse') -> getStoreWarehouse($store_id);
        if(!empty($warehouse_list)){
            foreach ($warehouse_list['data'] as $key => $value) {
                if($value['type'] == 1){//分店仓库
                    $store_warehouse_list[] = $value['wid'];
                }
            }
            $map = [];
            if($goods_id > 0)$map[] = ['goods_id', '=', $goods_id];
            $map[] = ['sku_id', '=', $sku_id];
            $map[] = ['warehouse_id', 'in', $store_warehouse_list[0]];
            $map[] = ['price', '>', 0];
            $stock_price = model('ErpGoodsWarehouseStockNum') ->field('min(price) as price') -> where($map) -> find();//库存价格信息

            if(!empty($stock_price)){
                $sku_price = $stock_price['price'];
            }
        }

        return array_out(200, 'success', $sku_price);
    }

    /**
     * 添加数据
     * @param warehouse_id			int			仓库id
     * @param sku_id 				int         sku_id
     * @param stock_num 		    int			库存数
     * @param available_num 		string      可操作数
     * @param goods_id 		        int 		商品id
     * @param price 		        string      售价
     * @param frozen_num 		    int 		冻结数
     * @author shiqiang
     * @return array_out(code, message, (int)id)
     */
    public function addData($warehouse_id, $sku_id, $stock_num, $available_num, $goods_id, $price, $frozen_num = 0){
        $data['warehouse_id']   = $warehouse_id;
        $data['sku_id']  		= $sku_id;
        $data['stock_num']    	= $stock_num;
        $data['available_num']	= $available_num;
        $data['goods_id']       = $goods_id;
        $data['price']         	= $price;
        $data['frozen_num']     = $frozen_num;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success', $this -> id);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 编辑数据
     * @param id 		        int     id
     * @param warehouse_id 	    int     仓库id
     * @param sku_id 	        int     货物id
     * @param stock_num 		int     库存总数
     * @param frozen_num 		int     冻结数
     * @param available_num	 	int     可操作数
     * @param price 		    float   售价
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function editData($id, $warehouse_id, $sku_id, $stock_num, $frozen_num = '', $available_num, $price = ''){
        $map = [];
        $map['id'] = $id;

        $data = [];
        $data['warehouse_id'] 	= $warehouse_id;
        $data['sku_id'] 	    = $sku_id;
        $data['stock_num'] 		= $stock_num;
        $data['available_num'] 	= $available_num;
		
		if($frozen_num != '')$data['frozen_num'] 	= $frozen_num;
        if($price != '')	 $data['price'] 	    = $price;
        $res = $this ->update($data, $map);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	

    /**
     * 编辑数据-出库使用
     * @param id 		        int     id
     * @param warehouse_id 	    int     仓库id
     * @param sku_id 	        int     货物id
     * @param stock_num 		int     库存总数
     * @param frozen_num 		int     冻结数
     * @param available_num	 	int     可操作数
     * @param price 		    float   售价
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, (int)id)
     */
    public function editDataForAllocation($warehouse_id, $sku_id, $frozen_num){
        $map = [];
        $map['warehouse_id'] = $warehouse_id;
        $map['sku_id'] = $sku_id;
        $res = Db::table($this->table)
            ->where($map)
            ->inc('frozen_num',$frozen_num)
            ->dec('available_num',$frozen_num)
            ->update();

            //dump($this->getLastSql());
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }


    /**
     * 分店商品库存展示
     * @param $id
     */
    public function index($store_id,$type)
    {
        $map = [];//搜索处理
        if($store_id > 0) 	    $map[] = ['sw.store_id', '=', $store_id];
        if($type > 0) 		    $map[] = ['w.type', '=', $type];
        $field = 'w.id,w.title,w.address,w.code as wcode,n.goods_id,n.sku_id,n.stock_num,n.frozen_num,n.available_num,s.*';
        $res = Db::table('xm_erp_warehouse')
            ->alias('w')
            ->join('xm_erp_goods_warehouse_stock_num n','w.id = n.warehouse_id')
            ->join('xm_erp_goods_sku s','s.id = n.sku_id AND s.goods_id = n.goods_id')
            ->join('xm_erp_store_warehouse sw','w.id = sw.warehouse_id')
            ->where($map)
            ->field($field)
            ->order('s.create_time desc')
            ->paginate();
        if($res){
            return array_out(200, 'success',$res);
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 关联 sku表
     */
    public function getErpGoodsSku(){
        return $this -> hasOne('app\erp\model\ErpGoodsSku','id', 'sku_id');
    }
}
